package com.c.yinyuezhushou.Adapter;

import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.c.yinyuezhushou.Fragment.SetupFragment;
import com.c.yinyuezhushou.MyApplication;
import com.c.yinyuezhushou.R;

import java.util.ArrayList;
import java.util.List;


public class SetupRecAdapter extends RecyclerView.Adapter {
    private MyApplication myApplication;
    private SetupFragment setupFragment;


    public SetupRecAdapter(MyApplication myApplication,SetupFragment setupFragment){
        this.myApplication = myApplication;
        this.setupFragment = setupFragment;
    }

    static class Rec2 extends RecyclerView.ViewHolder{
        Spinner spinner;
        TextView textView;
        public Rec2(@NonNull View itemView) {
            super(itemView);
            spinner = itemView.findViewById(R.id.isr_sp);
            textView = itemView.findViewById(R.id.isr_text1);
        }
    }

    static class Rec1 extends RecyclerView.ViewHolder{
        TextView textView1;
        TextView textView2;
        LinearLayout linearLayout;
        public Rec1(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.isr1_text1);
            textView2 = itemView.findViewById(R.id.isr1_text2);
            linearLayout = itemView.findViewById(R.id.is1r_lin1);
        }
    }

    static class Rec3 extends RecyclerView.ViewHolder{
        TextView textView;
        Switch aSwitch;
        public Rec3(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.isr3_text1);
            aSwitch = itemView.findViewById(R.id.isr3_switch);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        switch (viewType){
            case 0:
                View view=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setup_rec1,parent,false);
                Rec1 rec1 = new Rec1(view);
                return rec1;
            case 1:
                View view1=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setup_rec2,parent,false);
                Rec2 rec2 = new Rec2(view1);
                return rec2;
            case 2:
                View view2=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setup_rec3,parent,false);
                Rec3 rec3 = new Rec3(view2);
                return rec3;
            case 3:
                View view3=LayoutInflater.from(parent.getContext()).inflate(R.layout.item_setup_rec2,parent,false);
                Rec2 rec4 = new Rec2(view3);
                return rec4;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        List<String> list = new ArrayList<String>();
        list.add("标准音质");
        list.add("较高音质");
        list.add("极高音质");
        list.add("无损音质");
        switch (position){
            case 0:
                Rec1 rec1 = (Rec1)holder;
                rec1.textView1.setText("下载路径");
                rec1.textView2.setText(myApplication.getSetup().getPath());
                rec1.linearLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int v = Build.VERSION.SDK_INT;
                        if (v>20&&v<30){
                            setupFragment.FilePath(rec1.textView2);
                        }else {
                            Toast.makeText(myApplication, "安卓11暂不支持自定义路径，菜了菜了...", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
                break;
            case 1:
                Rec2 rec2 = (Rec2) holder;
                rec2.textView.setText("播放音质");
                rec2.spinner.setAdapter(new SpinnerAdapter(list));
                rec2.spinner.setSelection(myApplication.getSetup().getIntIndex(myApplication.getSetup().getPlaybr()),true);
                rec2.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        myApplication.getSetup().setPlaybr(myApplication.getSetup().getIntBr(i));
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });
                break;
            case 2:
                Rec3 rec3 = (Rec3) holder;
                rec3.textView.setText("下载前是否询问");
                rec3.aSwitch.setChecked(myApplication.getSetup().isDownAsk());
                rec3.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                        myApplication.getSetup().setDownAsk(b);
                    }
                });
                break;
            case 3:
                Rec2 rec4 = (Rec2) holder;
                rec4.textView.setText("默认下载音质");
                rec4.spinner.setAdapter(new SpinnerAdapter(list));
                rec4.spinner.setSelection(myApplication.getSetup().getIntIndex(myApplication.getSetup().getDownbr()),true);
                rec4.spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                        myApplication.getSetup().setDownbr(myApplication.getSetup().getIntBr(i));
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });
                break;

        }

    }

    @Override
    public int getItemCount() {
        return 4;
    }
}
